package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.*;
import com.zretc.entity.*;
import com.zretc.service.FrontMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王兴杭
 *前端电影的业务层实现类
 */
@Service
public class FrontMovieServiceImpl implements FrontMovieService {

    @Autowired
    private FilmTypeMapper filmTypeMapper;
    @Autowired
    private FilmMapper filmMapper;
    @Autowired
    private ActorMapper actorMapper;
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private FileAndCommentMapper fileAndCommentMapper;
    @Override
    public List<FilmType> findFileTypeName() {

        List<FilmType> filmTypeList = filmTypeMapper.findFilmTypeListByFilmTypeName(null);
        return filmTypeList;
    }

    @Override
    public List<String> findfilmReleaseTime() {
        List<String> ReleaseTime = filmMapper.findfilmReleaseTime();
        return ReleaseTime;
    }

    @Override
    public List<Actor> findActor() {
        List<Actor> actorNameList = actorMapper.findActorListByActorName(null);
        return actorNameList;
    }

    @Override
    public PageInfo<FilmAndComment> fidndfilm(Map<String, String> params) {

        Integer pageNum = Integer.valueOf(params.get("pageNum"));
        // 影片名称
        String filmName = params.get("filmName");
        // 上映时间
        String filmReleaseTime = params.get("filmReleaseTime");
        // 演员名称
        String actorName = params.get("actorName");
        // 类型名称
        String filmTypeName = params.get("filmTypeName");
        // 影片状态
        String filmStatus = params.get("filmStatus");

        Map<String, String> map = new HashMap<>();
        if ("".equals(filmName) || filmName == null) {
            map.put("filmName",null);
        } else {
            map.put("filmName","%" + filmName + "%");
        }
        if ("".equals(filmReleaseTime) || filmReleaseTime == null||"全部".equals(filmReleaseTime)) {
            map.put("filmReleaseTime",null);
        } else {
            map.put("filmReleaseTime","%" + filmReleaseTime + "%");
        }
        if ("".equals(actorName) || actorName == null||"全部".equals(actorName)) {
            map.put("actorName",null);
        } else {
            map.put("actorName","%" + actorName + "%");
        }
        if ("".equals(filmTypeName) || filmTypeName == null||"全部".equals(filmTypeName)) {
            map.put("filmTypeName",null);
        } else {
            map.put("filmTypeName","%" + filmTypeName + "%");
        }
        if ("".equals(filmStatus) || filmStatus == null) {
            map.put("filmStatus",null);
        } else {
            map.put("filmStatus",filmStatus);
        }

        // 开始分页
        PageHelper.startPage(pageNum,12);
        // 进行分页
        List<FilmAndComment> filmList = fileAndCommentMapper.findFilmByCondition(map);

        for (FilmAndComment f:filmList){
            List<Comments> comments = commentsMapper.findRootCommentsByFilmId(f.getFilmId());
            // 假设电影没评论
            if(comments.size() == 0){
                f.setScore("暂无分数");
            }else {
                Float total = 0.0f;
                for (Comments comment : comments) {
                    total += comment.getCommentsScore();
                }
                Float score = total / comments.size();
                f.setScore(score+"");
            }
        };
        // 用PageInfo对结果进行包装
        PageInfo<FilmAndComment> pageInfoFilm = new PageInfo<>(filmList);
        return pageInfoFilm;
    }
}
